﻿using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DragDemo;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private DragAdorner _currentAdorner;
    public MainWindow()
    {
        InitializeComponent();

        // 设置拖拽事件
        UploadArea.AllowDrop = true;
        UploadArea.DragEnter += UploadArea_DragEnter;
        UploadArea.DragLeave += UploadArea_DragLeave;
        UploadArea.Drop += UploadArea_Drop;
        UploadArea.DragOver += UploadArea_DragOver;
    }

    private void UploadArea_DragEnter(object sender, DragEventArgs e)
    {
        // 检查拖拽的是否是文件
        if (!e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effects = DragDropEffects.None;
            return;
        }

        // 显示装饰器
        var layer = AdornerLayer.GetAdornerLayer(UploadArea);
        _currentAdorner = new DragAdorner(UploadArea);
        layer.Add(_currentAdorner);

        // 改变鼠标样式
        e.Effects = DragDropEffects.Copy;

        StatusText.Text = "释放鼠标上传文件";
    }

    private void UploadArea_DragOver(object sender, DragEventArgs e)
    {
        // 保持效果
        e.Effects = DragDropEffects.Copy;
        e.Handled = true;
    }

    private void UploadArea_DragLeave(object sender, DragEventArgs e)
    {
        RemoveAdorner();
        StatusText.Text = "";
    }

    private void UploadArea_Drop(object sender, DragEventArgs e)
    {
        RemoveAdorner();

        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            foreach (string file in files)
            {
                try
                {
                    FileInfo fi = new FileInfo(file);
                    StatusText.Text = $"已接收文件: {fi.Name} ({FormatFileSize(fi.Length)})";

                    // 这里可以添加实际的上传逻辑
                    // UploadFile(file);
                }
                catch (Exception ex)
                {
                    StatusText.Text = $"错误: {ex.Message}";
                }
            }
        }
    }

    private void RemoveAdorner()
    {
        if (_currentAdorner != null)
        {
            var layer = AdornerLayer.GetAdornerLayer(UploadArea);
            layer.Remove(_currentAdorner);
            _currentAdorner = null;
        }
    }

    private string FormatFileSize(long bytes)
    {
        string[] sizes = { "B", "KB", "MB", "GB" };
        int order = 0;
        while (bytes >= 1024 && order < sizes.Length - 1)
        {
            order++;
            bytes = bytes / 1024;
        }
        return $"{bytes:0.##} {sizes[order]}";
    }
}
// 拖拽时的装饰器
public class DragAdorner : Adorner
{
    private readonly Brush _brush;

    public DragAdorner(UIElement adornedElement) : base(adornedElement)
    {
        _brush = new SolidColorBrush(Color.FromArgb(50, 100, 150, 255));
        _brush.Freeze(); // 提高性能
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        var adornedElementRect = new Rect(AdornedElement.RenderSize);

        // 绘制半透明背景
        drawingContext.DrawRectangle(_brush, null, adornedElementRect);

        // 绘制虚线边框
        var pen = new Pen(Brushes.DodgerBlue, 2)
        {
            DashStyle = DashStyles.Dash
        };
        pen.Freeze();

        drawingContext.DrawRectangle(null, pen, adornedElementRect);
    }
}